import datetime
import json

import scrapy
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from spiders.items import IndustryGrowthListItem


class StockGrowthListSpider(scrapy.Spider):
    '''
    行业涨幅排行    https://data.eastmoney.com/bkzj/BK0481.html
    '''
    name = 'stock_growth_list'

    industry_list = []

    def start_requests(self):
        '''
        代替start_urls,从数据库读取板块编码，拼接读取股票信息
        '''
        url = 'https://push2.eastmoney.com/api/qt/clist/get?' \
              'fid=f62' \
              '&po=1&pz=50&pn=1&np=1' \
              '&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5' \
              '&fs=b:{0}' \
              '&fields=f12,f14,f2,f3,f62'

        for industry in self.industry_list:
            stock_url = url.format(industry)
            if industry:
                meta = {'industry_code': industry}
                yield Request(stock_url, callback=self.parse, meta=meta)


    def __init__(self, industry_list):
        self.industry_list = industry_list

        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)


    def parse(self, response):
        text = response.text
        startIndex = text.find('{')
        endIndex = text.rfind('}')
        json_str = text[startIndex:endIndex + 1]
        dic_res = json.loads(json_str)
        data_ = dic_res['data']

        klines_list = data_['diff']
        if klines_list:
            for klines in klines_list:
                # 最新价格
                now_price = klines['f2']
                # 涨跌幅
                pctChg = klines['f3']
                # 换手率
                turn = klines['f8']
                # 量比
                volume_ratio = klines['f10']
                # 板块编码
                industry_code = klines['f12']
                # 板块名称
                industry_name = klines['f14']
                # 资金净流入
                main_net_inflow = klines['f62']
                # 领涨股票名称
                leading_stock_name = klines['f128']
                # 领涨股票代码
                leading_stock_code = klines['f140']
                # 上涨家数
                rising_nums = klines['f104']
                # 下跌家数
                decliner_nums = klines['f105']

                item = IndustryGrowthListItem()
                item['industry_name'] = industry_name  # '板块名称'
                item['industry_code'] = industry_code  # '板块code'
                item['now_price'] = now_price  # '最新价格'
                item['volume_ratio'] = volume_ratio  # '成交量（累计 单位：股）',
                item['pctChg'] = pctChg  # '涨跌幅',
                item['turn'] = turn  # '换手率',
                item['rising_nums'] = rising_nums  # '上涨家数',
                item['decliner_nums'] = decliner_nums  # '下跌家数',
                item['leading_stock_code'] = leading_stock_code  # '领涨股票',
                item['leading_stock_name'] = leading_stock_name  # '领涨股票',
                item['main_net_inflow'] = main_net_inflow  # '主力资金净流入',
                item['create_time'] = datetime.datetime.now()

                yield item
